package com.fht.controller.shop;


import com.fht.entity.MerchantsInformation;
import com.fht.entity.ShopAssistant;
import com.fht.entity.ShopTable;
import com.fht.from.ShopFrom;
import com.fht.service.food.MerchantsInformationService;
import com.fht.service.shop.ShopTableService;
import com.fht.util.DateTimeUtil;
import com.fht.util.ResultVOUtil;
import com.fht.vo.LayUITableVO;
import com.fht.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/shop/table")
public class ShopTableController {

    @Autowired
    public ShopTableService shopTableService;

    @Autowired
    public MerchantsInformationService merchantsInformationService;

    //菜品类别展示
    @GetMapping("/index")
    public String index(Model model){

        List<MerchantsInformation> merchantsInformationList = merchantsInformationService.findAllIdAndName();
        model.addAttribute("merchantsInformationList",merchantsInformationList);

        return "shop/table/index";

    }
    //获取菜品类别信息
    @GetMapping("/list")
    @ResponseBody
    public LayUITableVO<List<ShopTable>> list (@RequestParam(defaultValue = "1") Integer page,
                                                   @RequestParam(defaultValue = "10") Integer limit,
                                                   ShopFrom from){
        Map<String ,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("from",from);

        LayUITableVO<List<ShopTable>> vo = shopTableService.findPage(map);


        return vo;
    }

    //跳转到修改页面
    @GetMapping("/edit-ui")
    public String editUI(String id,Model model){

        ShopTable shopTable = shopTableService.selectById(id);

        List<MerchantsInformation> merchantsInformationList = merchantsInformationService.findAllIdAndName();
        model.addAttribute("merchantsInformationList",merchantsInformationList);

        model.addAttribute("shopTable",shopTable);

        return "shop/table/edit";

    }


    //修改map缓存中的数据，并重新计算价格总和
    @PostMapping("/edit")
    @ResponseBody
    public ResultVO edit (ShopTable shopTable){

        shopTableService.update(shopTable);

        return ResultVOUtil.ok();
    }

    //跳转到修改页面
    @GetMapping("/add-ui")
    public String addUI(String merchantsId,Model model){

        List<MerchantsInformation> merchantsInformationList = merchantsInformationService.findAllIdAndName();
        model.addAttribute("merchantsInformationList",merchantsInformationList);
        model.addAttribute("merchantsId",merchantsId);
        return "shop/table/add";

    }


    //修改map缓存中的数据，并重新计算价格总和
    @PostMapping("/add")
    @ResponseBody
    public ResultVO add ( ShopTable shopTable){


        shopTableService.save(shopTable);

        return ResultVOUtil.ok();
    }


    //修改map缓存中的数据，并重新计算价格总和
    @PostMapping("/del")
    @ResponseBody
    public ResultVO del ( String id){

        shopTableService.deleteById(id);

        return ResultVOUtil.ok();
    }
}
